//
//  CalendarAdaptor.m
//  YSCalendar
//
//  Created by qizhu on 15/3/17.
//  Copyright (c) 2015年 qizhu. All rights reserved.
//

#import "CalendarAdaptor.h"
#include "Augury.h"
#include "BubbleData.h"
#import "MainData.h"
#include <iostream>


@interface CalendarAdaptor() {
    Element5 element;
    Date birthDate;
    DateTime birthDateTime;
}

@end

/*******************************************
    c++ 代码部分
    int mon; // [0, 11]
    int day; // [1, 31]
 ***********************************************/
@implementation CalendarAdaptor

- (BOOL)isGoodDay4Act:(NSDateComponents *)date  act:(NSInteger)eventIdx withBirth:(NSDateComponents *)bDate andSex:(int)sex{
    auto tempDate = Date((int)date.year, (int)date.month-1, (int)date.day);
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    birthDateTime = DateTime(birthDate, (int)bDate.hour, (int)bDate.minute);
    element = calcuElement(birthDateTime);
    BOOL isGood = isGoodDay4Act(tempDate, element, (int)eventIdx,sex);
    return isGood;
}

- (NSString *)getDayTitle:(NSDateComponents *)date withBirth:(NSDateComponents *)bDate withSex: (BOOL)isMale {
    auto baseDate = Date((int)date.year, (int)date.month-1, (int)date.day);
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    int idx = getDayIndex(baseDate, birthDate);
    auto result = getDayTitle(idx, isMale);
    return [NSString stringWithUTF8String:result];
}

- (NSString *)getDaySummary:(NSDateComponents *)date male:(BOOL)isMale withBirth:(NSDateComponents *)bDate {
    auto baseDate = Date((int)date.year, (int)date.month-1, (int)date.day);
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    int idx = getDayIndex(baseDate, birthDate);
    auto result = getDaySummary(idx, isMale);
    return [NSString stringWithUTF8String:result];
}

- (NSString *)getDayDetail:(NSDateComponents *)date male:(BOOL)isMale withBirth:(NSDateComponents *)bDate {
    auto baseDate = Date((int)date.year, (int)date.month-1, (int)date.day);
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    int idx = getDayIndex(baseDate, birthDate);
    auto result = getDayDetail(idx, isMale);
    return [NSString stringWithUTF8String:result];
}

- (NSString *)getDayColor:(NSDateComponents *)date withBirth:(NSDateComponents *)bDate {
    auto baseDate = Date((int)date.year, (int)date.month-1, (int)date.day);
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    birthDateTime = DateTime(birthDate, (int)bDate.hour, (int)bDate.minute);
    element = calcuElement(birthDateTime);
    auto result = getDayColor(baseDate, element);
    return [NSString stringWithUTF8String:result];
}
- (NSString *)getStellarName:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    int idx = getStellarIndex(birthDate);
    auto name = getStellarName(idx);
    const char *result = name.c_str();
    return [NSString stringWithUTF8String:result];
}

- (NSString *)getStellaSub:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    int idx = getStellarIndex(birthDate);
    const char *result = getStellaSub(idx);
    return [NSString stringWithUTF8String:result];
}

- (NSString *)getStellaMeaning:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    int idx = getStellarIndex(birthDate);
    const char *result = getStellaMeaning(idx);
    return [NSString stringWithUTF8String:result];
}

- (NSString *)getStellaLife:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    int idx = getStellarIndex(birthDate);
    const char *result = getStellaLife(idx);
    return [NSString stringWithUTF8String:result];
}

- (NSString *)getStellaFate:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    int idx = getStellarIndex(birthDate);
    const char *result = getStellaFate(idx);
    return [NSString stringWithUTF8String:result];
}

- (NSString *)getStellaCharacter:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    int idx = getStellarIndex(birthDate);
    const char *result = getStellaCharacter(idx);
    return [NSString stringWithUTF8String:result];
}

- (NSString *)getStellaCareer:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    int idx = getStellarIndex(birthDate);
    const char *result = getStellaCareer(idx);
    return [NSString stringWithUTF8String:result];
}

- (NSString *)getStellaFortune:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    int idx = getStellarIndex(birthDate);
    const char *result = getStellaFortune(idx);
    return [NSString stringWithUTF8String:result];
}

- (NSString *)getStellaLove:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    int idx = getStellarIndex(birthDate);
    const char *result = getStellaLove(idx);
    return [NSString stringWithUTF8String:result];
}

- (NSString *)getStellaHealth:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    int idx = getStellarIndex(birthDate);
    const char *result = getStellaHealth(idx);
    return [NSString stringWithUTF8String:result];
}

- (NSInteger) getStellarIndex:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    int idx = getStellarIndex(birthDate);
    return idx/7;
}

- (NSString *)getYourGoodElement:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    birthDateTime = DateTime(birthDate, (int)bDate.hour, (int)bDate.minute);
    element = calcuElement(birthDateTime);
    NSArray *goodElement = @[@"木, 水, 火",
                             @"金,  土",
                             @"木, 土, 火",
                             @"金, 水",
                             @"土, 火, 金",
                             @"木, 水",
                             @"水, 土, 金",
                             @"火, 木",
                             @"木, 金, 水",
                             @"土, 火"];
    return goodElement[element];
}

- (NSString *)getElementName:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    birthDateTime = DateTime(birthDate, (int)bDate.hour, (int)bDate.minute);
    element = calcuElement(birthDateTime);
    NSLog(@"element--->");
    cout << birthDateTime.year << endl;
    cout << birthDateTime.mon << endl;
    cout << birthDateTime.day << endl;
    cout << element << endl;
    cout << element << endl;
    cout << element << endl;
    const char *result = getElementName(element);
    return [NSString stringWithUTF8String:result];
}

- (NSString *)getCharacterDesc:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    birthDateTime = DateTime(birthDate, (int)bDate.hour, (int)bDate.minute);
    int idx = getDaySB(birthDateTime);
    const char *result = getCharacterDesc(idx);
    return [NSString stringWithUTF8String:result];
}

- (NSString *)getLoveDesc:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    birthDateTime = DateTime(birthDate, (int)bDate.hour, (int)bDate.minute);
    int idx = getDaySB(birthDateTime);
    const char *result = getLoveDesc(idx);
    return [NSString stringWithUTF8String:result];

}

- (NSString *)getTemperDesc:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    birthDateTime = DateTime(birthDate, (int)bDate.hour, (int)bDate.minute);
    int idx = getMonSB(birthDateTime)%12;;
    const char *result = getTemperDesc(idx);
    return [NSString stringWithUTF8String:result];

}

- (NSString *)getWeakDesc:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    birthDateTime = DateTime(birthDate, (int)bDate.hour, (int)bDate.minute);
    int idx = getMonSB(birthDateTime)%12;
    const char *result = getWeakDesc(idx);
    return [NSString stringWithUTF8String:result];
    
}

- (NSString *)getElementDesc:(NSDateComponents *)bDate {
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    birthDateTime = DateTime(birthDate, (int)bDate.hour, (int)bDate.minute);
    int idx = getDaySB(birthDateTime)%10;
    const char *result = getElementDesc(idx);
    return [NSString stringWithUTF8String:result];
}
//判断生日有多少个knum
- (NSInteger)getDateNum:(NSDateComponents *)date withNum:(NSInteger)knum{

    NSInteger count = 0;
    NSString *str = [NSString stringWithFormat:@"%04ld%02ld%02ld",(long)date.year,(long)date.month,(long)date.day];
    NSInteger num = [str integerValue];
    for (int i=0; i<8; i++) {
        NSInteger k = (num%((NSInteger)pow(10, i+1)))/(pow(10, i));
        
        if  (k == knum) {
            count++;
        }
    }
    return count;
}

//const char* getThisWorldTitle(int oneCount, int sex)
//当世标题
- (NSString *)getThisWorldTitle:(NSDateComponents *)bDate male:(NSInteger)isMale
{
    int k =(int)[self getDateNum:bDate withNum:1];
    int male = (int)isMale;
    const char *result = getThisWorldTitle(k, male);
    return [NSString stringWithUTF8String:result];
}
//当世描述
- (NSString *)getThisWorldDesc:(NSDateComponents *)bDate male:(NSInteger)isMale
{
    int k =(int)[self getDateNum:bDate withNum:1];
    int male = (int)isMale;
    const char *result = getThisWorldDesc(k, male);
    return [NSString stringWithUTF8String:result];
}
//上一世你是什么
- (NSString *)getLastWorldTitle:(NSDateComponents *)bDate male:(NSInteger)isMale
{
    int k =(int)[self getDateNum:bDate withNum:1];
    int male = (int)isMale;
    const char *result = getLastWorldTitle(k, male);
    return [NSString stringWithUTF8String:result];
}
//上一世你的描述
- (NSString *)getLastWorldDesc:(NSDateComponents *)bDate male:(NSInteger)isMale
{
    int k =(int)[self getDateNum:bDate withNum:1];
    int male = (int)isMale;
    const char *result = getLastWorldDesc(k, male);
    return [NSString stringWithUTF8String:result];
}
//感情世界
- (NSString *)getLoveWorldDesc:(NSDateComponents *)bDate male:(NSInteger)isMale
{
    int k =(int)[self getDateNum:bDate withNum:1];
    int male = (int)isMale;
    const char *result = getLoveWorldDesc(k, male);
    return [NSString stringWithUTF8String:result];
}
//给你的锦囊
- (NSString *)getGiveYouTips:(NSDateComponents *)bDate male:(NSInteger)isMale
{
    int k =(int)[self getDateNum:bDate withNum:1];
    int male = (int)isMale;
    const char *result = getGiveYouTips(k, male);
    return [NSString stringWithUTF8String:result];
}

//感情表达能力
- (NSString *)getExpressionOfFeeling:(NSDateComponents *)bDate
{
    int k =(int)[self getDateNum:bDate withNum:1];
    
    if (k==0) {
        return @"";
    }else{
        const char *result = getExpressionOfFeeling(k);
        return [NSString stringWithUTF8String:result];
    }
    
}
//直觉度
- (NSString *)getIntuitionDegree:(NSDateComponents *)bDate
{
    int k =(int)[self getDateNum:bDate withNum:2];
    if (k==0) {
        return @"";
    }else{
        const char *result = getIntuitionDegree(k);
        return [NSString stringWithUTF8String:result];
    }
    
}
//思维能力和想象力
- (NSString *)getThinking:(NSDateComponents *)bDate
{
    int k =(int)[self getDateNum:bDate withNum:3];
    if (k==0) {
        return @"";
    }else{
        const char *result = getThinking(k);
        return [NSString stringWithUTF8String:result];
    }
    
}
//行动力
- (NSString *)getActivity:(NSDateComponents *)bDate
{
    int k =(int)[self getDateNum:bDate withNum:4];
    if (k==0) {
        return @"";
    }else{
        const char *result = getActivity(k);
        return [NSString stringWithUTF8String:result];
    }
}
//意志坚定度
- (NSString *)getFirmnessDegree:(NSDateComponents *)bDate
{
    int k =(int)[self getDateNum:bDate withNum:5];
    if (k==0) {
        return @"";
    }else{
        const char *result = getFirmnessDegree(k);
        return [NSString stringWithUTF8String:result];
    }
    
}
//自我价值
- (NSString *)getSelfValue:(NSDateComponents *)bDate
{
    int k =(int)[self getDateNum:bDate withNum:6];
    if (k==0) {
        return @"";
    }else{
        const char *result = getSelfValue(k);
        return [NSString stringWithUTF8String:result];
    }
    
}
//失恋治疗能力
- (NSString *)getLovelornTreat:(NSDateComponents *)bDate
{
    int k =(int)[self getDateNum:bDate withNum:7];
    if (k==0) {
        return @"";
    }else{
        const char *result = getLovelornTreat(k);
        return [NSString stringWithUTF8String:result];
    }
    
}
//智力和逻辑性
- (NSString *)getIntelligenceAndLogic:(NSDateComponents *)bDate
{
    int k =(int)[self getDateNum:bDate withNum:8];
    if (k==0) {
        return @"";
    }else{
        const char *result = getIntelligenceAndLogic(k);
        return [NSString stringWithUTF8String:result];
    }
    
}
//体贴度
- (NSString *)getConsiderateDegree:(NSDateComponents *)bDate
{
    int k =(int)[self getDateNum:bDate withNum:9];
    if (k==0) {
        return @"";
    }else{
        const char *result = getConsiderateDegree(k);
        return [NSString stringWithUTF8String:result];
    }
    
}
- (NSString *)getPositionWithBirthday:(NSDateComponents *)bDate today:(NSDateComponents *)date{
    const char *result = getPosition((int)date.year, (int)date.month, (int)date.day, (int)bDate.year, (int)bDate.month, (int)bDate.day, (int)bDate.hour);
    return [NSString stringWithUTF8String:result];
}
//计算好日子
- (int)getDayIsGoodDayIndex:(NSDateComponents *)date male:(int)isMale withBirth:(NSDateComponents *)bDate {
    auto baseDate = Date((int)date.year, (int)date.month-1, (int)date.day);
    auto baseDateb = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    int resultIndex = getDayIsGoodDay(baseDate, baseDateb, isMale);
    return resultIndex;
}

-(NSString *)getStrOfLunar:(NSDateComponents *)today{
    int x_year = (int)today.year;
    int x_month = (int)today.month;
    int x_day = (int)today.day;
    
    auto baseDate = Date(x_year, x_month-1, x_day);
    int yearIndex = getYearSB(baseDate);
    
    int monthIndex = getMonSB(baseDate);
    
    int dayIndex = getDaySB(baseDate);
    
    int hourIndex = getHourSB(baseDate, (int)today.hour);
    NSString *yearStr = [NSString stringWithUTF8String:getSBName(yearIndex)];
    NSString *monthStr = [NSString stringWithUTF8String:getSBName(monthIndex)];
    NSString *dayStr = [NSString stringWithUTF8String:getSBName(dayIndex)];
    NSString *hourStr = [NSString stringWithUTF8String:getSBName(hourIndex)];
    return [NSString stringWithFormat:@"%@年%@月%@日%@时",yearStr,monthStr,dayStr,hourStr];
}

-(NSString *)getDayTags:(NSDateComponents *)date male:(BOOL)isMale withBirth:(NSDateComponents *)bDate{
    
    auto baseDate = Date((int)date.year, (int)date.month-1, (int)date.day);
    auto baseDateb = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    const char *result = getDayTags(baseDate, baseDateb, isMale);
    return [NSString stringWithUTF8String:result];
}

-(NSString *)getSpecialDayTags:(NSDateComponents *)bDate type:(int)type {
    auto baseDateb = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    const char *result = getSpecialDayTags(baseDateb, type);
    return [NSString stringWithUTF8String:result];
}

-(int)getDayScore:(NSDateComponents *)date male:(BOOL)isMale withBirth:(NSDateComponents *)bDate{
    auto baseDate = Date((int)date.year, (int)date.month-1, (int)date.day);
    auto baseDateb = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    return getDayScore(baseDate, baseDateb, isMale);
}
-(NSString *)getStellarCharactersTags:(NSDateComponents *)bDate{
    auto baseDateb = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    const char *result = getStellarCharactersTags(baseDateb);
    return [NSString stringWithUTF8String:result];
}


-(NSString *)getStellarStudyTags:(NSDateComponents *)bDate{
    auto baseDateb = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    const char *result = getStellarStudyTags(baseDateb);
    return [NSString stringWithUTF8String:result];
}
-(NSString *)getStellarLoveTags:(NSDateComponents *)bDate{
    auto baseDateb = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    const char *result = getStellarLoveTags(baseDateb);
    return [NSString stringWithUTF8String:result];
}
-(NSString *)getStellarFortuneTags:(NSDateComponents *)bDate{
    auto baseDateb = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    const char *result = getStellarFortuneTags(baseDateb);
    return [NSString stringWithUTF8String:result];
}
-(NSString *)getStellarHealthTags:(NSDateComponents *)bDate{
    auto baseDateb = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    const char *result = getStellarHealthTags(baseDateb);
    return [NSString stringWithUTF8String:result];
}
-(NSString *)getStellarDesc:(NSDateComponents *)bDate{
    auto baseDateb = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    const char *result = getStellarDesc(baseDateb);
    return [NSString stringWithUTF8String:result];
}
//获取金钱观数据
-(NSString *)getMoneyView:(NSDateComponents *)bDate{
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    birthDateTime = DateTime(birthDate, (int)bDate.hour, (int)bDate.minute);
    int idx = getDaySB(birthDateTime);
    const char *result = getMoneyView(idx);
    return [NSString stringWithUTF8String:result];
}
//获取先天财气数据
-(NSString *)getCongenitalMoney:(NSDateComponents *)bDate{
    birthDate = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    birthDateTime = DateTime(birthDate, (int)bDate.hour, (int)bDate.minute);
    int idx = getDaySB(birthDateTime);
    const char *result = getCongenitalMoney(idx);
    return [NSString stringWithUTF8String:result];
}
//获取当天的十神
-(NSString *)getDaySBName:(NSDateComponents *)date withBirth:(NSDateComponents *)bDate{
    auto baseDate = Date((int)date.year, (int)date.month-1, (int)date.day);
    auto baseDateb = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    const char *result = getDaySBName(baseDate, baseDateb);
    return [NSString stringWithUTF8String:result];
}
-(NSString *)getDayEvent:(NSDateComponents *)date male:(BOOL)isMale withBirth:(NSDateComponents *)bDate{
    auto baseDate = Date((int)date.year, (int)date.month-1, (int)date.day);
    auto baseDateb = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    const char *result = getDayEvent(baseDate, baseDateb, isMale);
    return [NSString stringWithUTF8String:result];
}

-(NSString *)getDayGoodOrBad:(NSDateComponents *)date male:(BOOL)isMale withBirth:(NSDateComponents *)bDate {
    auto baseDate = Date((int)date.year, (int)date.month-1, (int)date.day);
    auto baseDateb = Date((int)bDate.year, (int)bDate.month-1, (int)bDate.day);
    const char *result = getDayGoodOrBad(baseDate, baseDateb, isMale);
    return [NSString stringWithUTF8String:result];
}



//获取当前节气一句话总结
-(NSString *)getFestivalTitle:(NSInteger) index {
    const char * result = getFestivalTitle((int)index);
    return [NSString stringWithUTF8String:result];
}

//获取当前节气详情
-(NSString *)getFestivalDesc:(NSInteger) index {
    const char * desc = getFestivalDesc((int)index);
    return [NSString stringWithUTF8String:desc];
}

//获取农历节日的描述
-(NSString *)getSpecFestivalDesc:(NSInteger) index {
    const char * desc = getSpecFestivalDescByDateStr((int)index);
    return [NSString stringWithUTF8String:desc];
}


@end
